#include <am.h>
#include <nemu.h>

#ifdef MODE_800x600
#define W  800
#define H  600
#else
#define W 400
#define H 300
#endif

#define SYNC_ADDR (VGACTL_ADDR + 4)

void __am_gpu_init() {
}

void __am_gpu_config(AM_GPU_CONFIG_T *cfg) {
  *cfg = (AM_GPU_CONFIG_T) {
    .present = true, .has_accel = false,
    .width = W, .height = H, 
    .vmemsz = 0
  };
}

void __am_gpu_fbdraw(AM_GPU_FBDRAW_T *ctl) {
  if (ctl->sync) {
    outl(SYNC_ADDR, 1);
  }

  uint32_t *fb = (uint32_t *)(uintptr_t)FB_ADDR;

  int i = 0;
  for (int y = ctl->y; y < ctl->y + ctl->h; y++) {
    for (int x = ctl->x; x < ctl->x + ctl->w; x++) {
      fb[x + y * 400] = ((uint32_t *)ctl->pixels)[i++];
    }
  }
}

void __am_gpu_status(AM_GPU_STATUS_T *status) {
  status->ready = true;
}
